<template>
  <el-dialog
    title="云存储配置"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item size="mini" label="存储类型">
        <el-radio-group v-model="dataForm.type">
          <el-radio :label="1">七牛</el-radio>
          <el-radio :label="2">阿里云</el-radio>
          <el-radio :label="3">腾讯云</el-radio>
          <el-radio :label="4">FastDFS</el-radio>
          <el-radio :label="5">本地上传</el-radio>
        </el-radio-group>
      </el-form-item>
      <template v-if="dataForm.type === 1">
        <el-form-item label="域名" prop="qiniuDomain">
          <el-input v-model="dataForm.qiniuDomain" placeholder="七牛绑定的域名"></el-input>
        </el-form-item>
        <el-form-item label="路径前缀" prop="qiniuPrefix">
          <el-input v-model="dataForm.qiniuPrefix" placeholder="不设置默认为空"></el-input>
        </el-form-item>
        <el-form-item label="AccessKey" prop="qiniuAccessKey">
          <el-input v-model="dataForm.qiniuAccessKey" placeholder="七牛AccessKey"></el-input>
        </el-form-item>
        <el-form-item label="SecretKey" prop="qiniuSecretKey">
          <el-input v-model="dataForm.qiniuSecretKey" placeholder="七牛SecretKey"></el-input>
        </el-form-item>
        <el-form-item label="空间名" prop="qiniuBucketName">
          <el-input v-model="dataForm.qiniuBucketName" placeholder="七牛存储空间名"></el-input>
        </el-form-item>
      </template>
      <template v-else-if="dataForm.type === 2">
        <el-form-item label="域名" prop="aliyunDomain">
          <el-input v-model="dataForm.aliyunDomain" placeholder="阿里云绑定的域名，如：http://cdn.renren.io"></el-input>
        </el-form-item>
        <el-form-item label="路径前缀" prop="aliyunPrefix">
          <el-input v-model="dataForm.aliyunPrefix" placeholder="不设置默认为空"></el-input>
        </el-form-item>
        <el-form-item label="EndPoint" prop="aliyunEndPoint">
          <el-input v-model="dataForm.aliyunEndPoint" placeholder="阿里云EndPoint"></el-input>
        </el-form-item>
        <el-form-item label="AccessKeyId" prop="aliyunAccessKeyId">
          <el-input v-model="dataForm.aliyunAccessKeyId" placeholder="阿里云AccessKeyId"></el-input>
        </el-form-item>
        <el-form-item label="AccessKeySecret" prop="aliyunAccessKeySecret">
          <el-input v-model="dataForm.aliyunAccessKeySecret" placeholder="阿里云AccessKeySecret"></el-input>
        </el-form-item>
        <el-form-item label="BucketName" prop="aliyunBucketName">
          <el-input v-model="dataForm.aliyunBucketName" placeholder="阿里云BucketName"></el-input>
        </el-form-item>
      </template>
      <template v-else-if="dataForm.type === 3">
        <el-form-item label="域名" prop="qcloudDomain">
          <el-input v-model="dataForm.qcloudDomain" placeholder="腾讯云绑定的域名"></el-input>
        </el-form-item>
        <el-form-item label="路径前缀" prop="qcloudPrefix">
          <el-input v-model="dataForm.qcloudPrefix" placeholder="不设置默认为空"></el-input>
        </el-form-item>
        <el-form-item label="AppId" prop="qcloudAppId">
          <el-input v-model="dataForm.qcloudAppId" placeholder="腾讯云AppId"></el-input>
        </el-form-item>
        <el-form-item label="SecretId" prop="qcloudSecretId">
          <el-input v-model="dataForm.qcloudSecretId" placeholder="腾讯云SecretId"></el-input>
        </el-form-item>
        <el-form-item label="SecretKey" prop="qcloudSecretKey">
          <el-input v-model="dataForm.qcloudSecretKey" placeholder="腾讯云SecretKey"></el-input>
        </el-form-item>
        <el-form-item label="BucketName" prop="qcloudBucketName">
          <el-input v-model="dataForm.qcloudBucketName" placeholder="腾讯云BucketName"></el-input>
        </el-form-item>
        <el-form-item label="所属地区" prop="qcloudRegion">
          <el-select v-model="dataForm.qcloudRegion" clearable placeholder="请选择">
            <el-option label="北京一区（华北）" value="ap-beijing-1"></el-option>
            <el-option label="北京" value="ap-beijing"></el-option>
            <el-option label="上海（华东）" value="ap-shanghai"></el-option>
            <el-option label="广州（华南）" value="ap-guangzhou"></el-option>
            <el-option label="成都（西南）" value="ap-chengdu"></el-option>
            <el-option label="重庆" value="ap-chongqing"></el-option>
            <el-option label="新加坡" value="ap-singapore"></el-option>
            <el-option label="香港" value="ap-hongkong"></el-option>
            <el-option label="多伦多" value="na-toronto"></el-option>
            <el-option label="法兰克福" value="eu-frankfurt"></el-option>
          </el-select>
        </el-form-item>
      </template>
      <template v-else-if="dataForm.type === 4">
        <el-form-item label="域名" prop="localDomain">
          <el-input v-model="dataForm.localDomain" placeholder="绑定的域名，如：http://cdn.renren.io"></el-input>
        </el-form-item>
      </template>
      <template v-else-if="dataForm.type === 5">
        <el-form-item label="域名" prop="fastdfsDomain">
          <el-input v-model="dataForm.fastdfsDomain" placeholder="绑定的域名，如：http://cdn.renren.io"></el-input>
        </el-form-item>
        <el-form-item label="路径前缀" prop="localPrefix">
          <el-input v-model="dataForm.localPrefix" placeholder="不设置默认为空"></el-input>
        </el-form-item>
        <el-form-item label="存储目录" prop="localPath">
          <el-input v-model="dataForm.localPath" placeholder="如：D:/upload"></el-input>
        </el-form-item>
      </template>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          type: 0,
          qiniuDomain: '',
          qiniuPrefix: '',
          qiniuAccessKey: '',
          qiniuSecretKey: '',
          qiniuBucketName: '',
          aliyunDomain: '',
          aliyunPrefix: '',
          aliyunEndPoint: '',
          aliyunAccessKeyId: '',
          aliyunAccessKeySecret: '',
          aliyunBucketName: '',
          qcloudDomain: '',
          qcloudPrefix: '',
          qcloudAppId: 0,
          qcloudSecretId: '',
          qcloudSecretKey: '',
          qcloudBucketName: '',
          qcloudRegion: '',
          localDomain: '',
          fastdfsDomain: '',
          localPrefix: '',
          localPath: ''
        },
        dataRule: {
          qiniuDomain: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          qiniuAccessKey: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          qiniuSecretKey: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          qiniuBucketName: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          aliyunDomain: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          aliyunEndPoint: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          aliyunAccessKeyId: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          aliyunAccessKeySecret: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          aliyunBucketName: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          qcloudDomain: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          qcloudAppId: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          qcloudSecretId: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          qcloudSecretKey: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          qcloudBucketName: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          qcloudRegion: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          localDomain: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          fastdfsDomain: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          localPath: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    watch: {
      'dataForm.type' (val) {
        this.$refs['dataForm'].clearValidate()
      }
    },
    methods: {
      init () {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.getConfig()
        })
      },
      // 获取云存储配置
      getConfig () {
        this.$http.get(`${window.SITE_CONFIG['baseURL']}/sys/oss/config`).then(({data}) => {
          if (data && data.code === 0) {
            this.dataForm.type = data.config.type
            this.dataForm.qiniuDomain = data.config.qiniuDomain
            this.dataForm.qiniuPrefix = data.config.qiniuPrefix
            this.dataForm.qiniuAccessKey = data.config.qiniuAccessKey
            this.dataForm.qiniuSecretKey = data.config.qiniuSecretKey
            this.dataForm.qiniuBucketName = data.config.qiniuBucketName
            this.dataForm.aliyunDomain = data.config.aliyunDomain
            this.dataForm.aliyunPrefix = data.config.aliyunPrefix
            this.dataForm.aliyunEndPoint = data.config.aliyunEndPoint
            this.dataForm.aliyunAccessKeyId = data.config.aliyunAccessKeyId
            this.dataForm.aliyunAccessKeySecret = data.config.aliyunAccessKeySecret
            this.dataForm.aliyunBucketName = data.config.aliyunBucketName
            this.dataForm.qcloudDomain = data.config.qcloudDomain
            this.dataForm.qcloudPrefix = data.config.qcloudPrefix
            this.dataForm.qcloudAppId = data.config.qcloudAppId
            this.dataForm.qcloudSecretId = data.config.qcloudSecretId
            this.dataForm.qcloudSecretKey = data.config.qcloudSecretKey
            this.dataForm.qcloudBucketName = data.config.qcloudBucketName
            this.dataForm.qcloudRegion = data.config.qcloudRegion
            this.dataForm.localDomain = data.config.localDomain
            this.dataForm.fastdfsDomain = data.config.fastdfsDomain
            this.dataForm.localPrefix = data.config.localPrefix
            this.dataForm.localPath = data.config.localPath
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http.post(
              `${window.SITE_CONFIG['baseURL']}/sys/oss/saveConfig`,
              {
                type: this.dataForm.type,
                qiniuDomain: this.dataForm.qiniuDomain,
                qiniuPrefix: this.dataForm.qiniuPrefix,
                qiniuAccessKey: this.dataForm.qiniuAccessKey,
                qiniuSecretKey: this.dataForm.qiniuSecretKey,
                qiniuBucketName: this.dataForm.qiniuBucketName,
                aliyunDomain: this.dataForm.aliyunDomain,
                aliyunPrefix: this.dataForm.aliyunPrefix,
                aliyunEndPoint: this.dataForm.aliyunEndPoint,
                aliyunAccessKeyId: this.dataForm.aliyunAccessKeyId,
                aliyunAccessKeySecret: this.dataForm.aliyunAccessKeySecret,
                aliyunBucketName: this.dataForm.aliyunBucketName,
                qcloudDomain: this.dataForm.qcloudDomain,
                qcloudPrefix: this.dataForm.qcloudPrefix,
                qcloudAppId: this.dataForm.qcloudAppId,
                qcloudSecretId: this.dataForm.qcloudSecretId,
                qcloudSecretKey: this.dataForm.qcloudSecretKey,
                qcloudBucketName: this.dataForm.qcloudBucketName,
                qcloudRegion: this.dataForm.qcloudRegion,
                localDomain: this.dataForm.localDomain,
                fastdfsDomain: this.dataForm.fastdfsDomain,
                localPrefix: this.dataForm.localPrefix,
                localPath: this.dataForm.localPath
              }
            ).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .mod-oss__oss {
    .el-select {
      width: 100%;
    }
  }
</style>
